//
//  ViewController.swift
//  LKSportSDKSportManager
//
//  Created by Yue Kun on 08/23/2023.
//  Copyright (c) 2023 Yue Kun. All rights reserved.
//

import UIKit
import LKSportSDKSportManager
class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    // 打印数据
    var dataArray: [String] = []
    // 单人运动
    var sportEngine = SingleSportEngine(userConfig: UserConfig(userid: "lk01"))
    // 多人运动
    var multipleSportEngine: [SingleSportEngine] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        makeUI()
        //单人运动
        singleSport()
        //多人同时开始运动
        multipleSport()
    }
    func makeUI() {
        self.tableView .register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
    }
    
    @IBAction func buttoneAction(_ sender: Any) {
        let btn = sender as? UIButton
        let tag = btn?.tag
        if tag == 1 {
            sportEngine.startSport()
        }
        if tag == 2 {
            sportEngine.pauseSport()
        }
        if tag == 3 {
            sportEngine.continueSport()
        }
        if tag == 4 {
            sportEngine.endSport()
        }
        if tag == 5 {
            for engine in multipleSportEngine {
                engine.startSport()
            }
        }
        if tag == 6 {
            for engine in multipleSportEngine {
                engine.endSport()
            }
        }
    }
    func singleSport() {
        sportEngine.sportDataEngine = DefaultSportDataEngine()
        sportEngine.calculateEngine = DefaultSportCalculateEngine(user: UserConfig(userid: "lk01"))
        sportEngine.calculateEngine?.delegate = self
    }
    func multipleSport() {
        // 运动员 小明
        let xiaoMing = SingleSportEngine(userConfig: UserConfig(userid: "lk02"))
        xiaoMing.sportDataEngine = DefaultSportDataEngine()
        xiaoMing.calculateEngine = DefaultSportCalculateEngine(user: UserConfig(userid: "lk02"))
        xiaoMing.calculateEngine?.delegate = self
        multipleSportEngine.append(xiaoMing)
        // 运动员 李雷
        let liLei = SingleSportEngine(userConfig: UserConfig(userid: "lk03"))
        liLei.sportDataEngine = DefaultSportDataEngine()
        liLei.calculateEngine = DefaultSportCalculateEngine(user: UserConfig(userid: "lk03"))
        liLei.calculateEngine?.delegate = self
        multipleSportEngine.append(liLei)
    }
}

extension ViewController: SportDataCalculateEngineDelegate {
    func calculateCompletion(user: UserConfig, record: SportRecordModel) {
        printLogs(str: "\(record.description)")
    }
    
    func heartRateLevelUpgrad(user: UserConfig, level: SportHeartRateLevel) {
        printLogs(str: "心率升级了⬆️⬆️⬆️⬆️")
    }
    
    func heartRateLevelReduce(user: UserConfig, level: SportHeartRateLevel) {
        printLogs(str: "心率降级了 ⬇️⬇️⬇️⬇️")
    }
    
    func onEnterFireState(user: UserConfig) {
        printLogs(str: "火力全开🔥🔥🔥🔥")
    }
    
    func onExitFireState(user: UserConfig) {
        printLogs(str: "火力全开结束🧯🧯🧯🧯")
    }
    func printLogs(str: String) {
        self.dataArray.append(str)
        tableView.reloadData()
        tableView.scrollToRow(at: IndexPath(row: dataArray.count - 1, section: 0), at: .none, animated: true)
        print(str)
    }
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 20
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        let text = dataArray[indexPath.row]
        cell.contentView.backgroundColor = .black
        cell.textLabel?.text = text
        cell.textLabel?.textColor = .white
        cell.textLabel?.font = .systemFont(ofSize: 12)
        return cell
    }
}
